package com.spring.springmvc.请求.传参;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Arrays;

@RequestMapping("/cookie")
@RestController
public class 获取cookie {
    //方法1
@RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request, HttpServletResponse response){
    Cookie[] cookies = request.getCookies();
    //打印获取的cookie
    if(cookies!=null){
        for (Cookie cookie:cookies){
            System.out.println(cookie.getName()+"="+cookie.getValue());
        }
    }
    return "获取成功";
}
    //方法2
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("name") String na){
        return "获取cookie:"+na;
    }

}
